/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : GetCountryAvailability
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Country
 */
class CountryAvailability{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid         = null;
	      string partnerPassword	   = null;
	      string country		       = null;	
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid      = args[0].Trim();
	      partnerPassword    = args[1].Trim();
	      country    	     = args[2].Trim();
	      
	      GraydonCompanyData                 graydonCompanyData               = new GraydonCompanyData();
	      GetCountryAvailability_ResultType  getCountryAvailability_Result    = null;
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                   = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                   = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                 = partnerPassword;
	      
	      // create the parameters
	      GetCountryAvailability_ParametersType getCountryAvailability_Parameters                   = new GetCountryAvailability_ParametersType();
	      getCountryAvailability_Parameters.Authentication_Parameters                               = authentication_Parameters;
	      getCountryAvailability_Parameters.Country                                                 = country;
	      
	      // Begin the Async call to "getCountryAvailability"
	      IAsyncResult ar = graydonCompanyData.BegingetCountryAvailability(getCountryAvailability_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      try
          {
	         // get the results
	         getCountryAvailability_Result = graydonCompanyData.EndgetCountryAvailability(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }      
          
         // get the service log details
         serviceLog(getCountryAvailability_Result.Service_Log);
         
		 // check if the country availability is populated
		 if (getCountryAvailability_Result.CountryAvailablity != null)
		 {		
			// get the country company match methods
			CountryAndProductType[] countryAndProduct = getCountryAvailability_Result.CountryAvailablity;
			
			for (int x = 0; x < countryAndProduct.Length; x++)
			{
				Console.WriteLine("Availability:");
				Console.WriteLine("Country\t\t\t: " + countryAndProduct[x].Country);	
				
				// get the match methods
				ProductType[] products = countryAndProduct[x].Products;
				for (int y = 0; y < products.Length; y++)
				{
					Console.WriteLine("  Product: ");
					Console.WriteLine("  Type\t\t\t: " + products[y].Type);
					Console.WriteLine("  Code\t\t\t: " + products[y].Code);	
					Console.WriteLine("  Level\t\t\t: " + products[y].Level);
					Console.WriteLine("  Days From\t\t: " + products[y].DaysFrom);
					Console.WriteLine("  Days To\t\t: " + products[y].DaysTo);
					Console.WriteLine("  Service Speed\t\t: " + products[y].ServiceSpeed);
					
				}
				Console.WriteLine(" ");
			}
		}
	  }  
      catch (Exception e)
      {
	     Console.WriteLine("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}   
   
}